package cn.edu.pku.ss.music.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.ss.music.R;
import cn.edu.pku.ss.music.net.LoginTask;
import cn.edu.pku.ss.music.util.ConnectionURLs;
import cn.edu.pku.ss.music.util.Util;

public class LoginActivity extends Activity {
	private String username = "";
	private String password = "";
	private static final String url = ConnectionURLs.LOGIN;
	
	private Button submit_button;
	private EditText username_edittext;
	private EditText password_edittext;
	private TextView title_textview;
	
	private LoginTask conn_task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.login_layout);
		submit_button = (Button) findViewById(R.id.submit);
		username_edittext = (EditText) findViewById(R.id.username_edittext);
		password_edittext = (EditText) findViewById(R.id.password_edittext);
		
		
		View layout = findViewById(R.id.login_header);
		title_textview = (TextView) layout.findViewById(R.id.header_title);
		title_textview.setText(R.string.login_title);
		
		submit_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				username = username_edittext.getText().toString();
				password = password_edittext.getText().toString();
				
				if((!Util.isEmpty(username)) && (!Util.isEmpty(password))){
					conn_task = new LoginTask();
					conn_task.execute(url, username, password);
				}
				else if(Util.isEmpty(username)){
					Toast.makeText(LoginActivity.this, R.string.empty_username, 
							Toast.LENGTH_SHORT).show();
					return;
				}
				else if(Util.isEmpty(password)){
					Toast.makeText(LoginActivity.this, R.string.empty_password, 
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
		});
	}
}
